<?php

namespace App\Console\Commands;

use App\Models\User;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Redis;

class RecordLastLogin extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'laravel:record_last_login';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle(User $user)
    {
        $key = 'last_login_'.Carbon::now()->toDateString();
        $data = Redis::hGetAll($key);
        foreach ($data as $key=>$value) {
            $id = str_replace('user_','',$key);
            $user = User::query()->find($id);
            $user->last_login_time = $value;
            $user->save();
        }

    }
}
